unit EditTournamentRounds;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, uutils, gnugettext, Grids, umainutils;

type
  TuFrmEditTournamentRounds = class(TForm)
    edt_Sequence: TLabeledEdit;
    edt_Duration: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    chb_IsBreak: TCheckBox;
    edt_SmallBlind: TLabeledEdit;
    edt_BigBlind: TLabeledEdit;
    Label1: TLabel;
    edt_Ante: TLabeledEdit;
    chb_LastRebuyRound: TCheckBox;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure chb_IsBreakClick(Sender: TObject);
  private
    { Private declarations }
    m_IsClone, m_IsEdit, m_RebuyAllowed, m_IsRoundTypes: boolean;
    m_recordID: string;
  public
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string; IsRoundTypes: boolean = false);
    { Public declarations }
  end;

var
  uFrmEditTournamentRounds: TuFrmEditTournamentRounds;

implementation

uses UfrmMain;
{$R *.dfm}
{ TuFrmEditTournamentRounds }

procedure TuFrmEditTournamentRounds.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditTournamentRounds.btn_SaveClick(Sender: TObject);
var
  SmallB, BigB: string;
  SelRow, i: integer;
  RebuyStr, CellValue, MasterDataID: string;
  TournamentIdOrRoundType, TableNm, ForeignKeyName: string;
  Grid: TStringGrid;
begin
  if m_IsRoundTypes then
  begin
    TableNm := 'TOURNAMENT_ROUND_TYPE_DETAILS';
    ForeignKeyName := 'ROUND_TYPE_ID';
    Grid := FrmMain.stg_RoundTypes;
  end
  else
  begin
    TableNm := 'TOURNAMENT_ROUNDS';
    TournamentIdOrRoundType := FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row];
    ForeignKeyName := 'TOURNAMENT_ID';
    Grid := FrmMain.stg_Tournaments;
  end;
  if chb_IsBreak.Checked then
  begin
    SmallB := '';
    BigB := '';
  end
  else
  begin
    SmallB := edt_SmallBlind.Text;
    BigB := edt_BigBlind.Text;
  end;
  MasterDataID := Grid.Cells[0, Grid.Row];
  if m_IsClone or (not m_IsEdit) then
  begin
    m_recordID := InsertDataIntoDB(TableNm, 'DURATION, IS_BREAK, SEQ_INDEX, SMALL_BLIND, BIG_BLIND, ANTE, ' + ForeignKeyName, '''' + edt_Duration.Text + ''', ''' + BToStr(chb_IsBreak.Checked)
        + ''', ''' + edt_Sequence.Text + ''', ''' + SmallB + ''', ''' + BigB + ''', ''' + edt_Ante.Text + ''', ''' + MasterDataID + '''');
  end
  else
  begin
    UpdateDataToDB(TableNm, 'DURATION=''' + edt_Duration.Text + ''',SEQ_INDEX=''' + edt_Sequence.Text + ''',IS_BREAK=''' + BToStr(chb_IsBreak.Checked)
        + ''',SMALL_BLIND=''' + SmallB + ''',BIG_BLIND=''' + BigB + ''',ANTE=''' + edt_Ante.Text + '''', 'ID=' + m_recordID);
  end;
  // set rebuy rounds
  if m_IsRoundTypes then
  begin
    Grid := FrmMain.stg_RoundTypeRounds;
  end
  else
  begin
    Grid := FrmMain.stg_TournamentRounds;
  end;
  if m_RebuyAllowed or (Grid = FrmMain.stg_RoundTypeRounds) then
  begin
    // update to null
    for i := 2 to Grid.RowCount do
    begin
      CellValue := Grid.Cells[1, i];
      if CellValue = '' then
      begin
        CellValue := '0';
      end;
      RebuyStr := '';
      if chb_LastRebuyRound.Checked then
      begin
        if StrToInt(edt_Sequence.Text) > StrToInt(CellValue) then
        begin
          RebuyStr := 'REBUY'
        end
        else if StrToInt(edt_Sequence.Text) + 1 = StrToInt(CellValue) then
        begin
          RebuyStr := 'ADDON'
        end;
      end;
      CellValue := Grid.Cells[1, i];
      if CellValue = '' then
      begin
        CellValue := edt_Sequence.Text;
      end;
      UpdateDataToDB(TableNm, 'REBUY_OR_ADDON=' + '''' + RebuyStr + '''', ForeignKeyName + '=' + MasterDataID + ' AND SEQ_INDEX=' + CellValue);
    end;
  end;
  // ----
  if not m_IsRoundTypes then
  begin
    SelRow := FrmMain.stg_TournamentRounds.Row;
    FrmMain.GetGridData(FrmMain.stg_TournamentRounds, SQL_TournamentRounds + FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row] + ' ORDER BY SEQ_INDEX ASC', false);
    if m_IsClone or (not m_IsEdit) then
    begin
      FrmMain.SelectGridRow(FrmMain.stg_TournamentRounds, FrmMain.stg_TournamentRounds.RowCount - 1);
    end
    else
    begin
      FrmMain.SelectGridRow(FrmMain.stg_TournamentRounds, SelRow);
    end;
  end
  else
  begin
    SelRow := FrmMain.stg_RoundTypeRounds.Row;
    FrmMain.GetGridData(FrmMain.stg_RoundTypeRounds, SQL_RoundTypeDetails + FrmMain.stg_RoundTypes.Cells[0, FrmMain.stg_RoundTypes.Row] + ' ORDER BY SEQ_INDEX ASC', false);
    if m_IsClone or (not m_IsEdit) then
    begin
      FrmMain.SelectGridRow(FrmMain.stg_RoundTypeRounds, FrmMain.stg_RoundTypeRounds.RowCount - 1);
    end
    else
    begin
      FrmMain.SelectGridRow(FrmMain.stg_RoundTypeRounds, SelRow);
    end;
  end;
  close();
end;

procedure TuFrmEditTournamentRounds.chb_IsBreakClick(Sender: TObject);
begin
  edt_SmallBlind.Visible := NOT chb_IsBreak.Checked;
  edt_BigBlind.Visible := NOT chb_IsBreak.Checked;
  edt_Ante.Visible := NOT chb_IsBreak.Checked;
end;

procedure TuFrmEditTournamentRounds.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
end;

procedure TuFrmEditTournamentRounds.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditTournamentRounds.FormShow(Sender: TObject);
begin
  edt_Duration.SetFocus;
end;

procedure TuFrmEditTournamentRounds.ShowForm(IsEdit, IsClone: boolean; recordID: string; IsRoundTypes: boolean = false);
var
  Query: string;
  SequenceIdx: integer;
  TableNm: string;
  Grid: TStringGrid;
begin
  m_IsClone := IsClone;
  m_IsEdit := IsEdit;
  m_recordID := recordID;
  m_IsRoundTypes := IsRoundTypes;
  OracleQuery1.Session := FrmMain.OracleSession1;
  if not m_IsRoundTypes then
  begin
    if FrmMain.CheckSoldTicketForTournament(FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row]) then
    begin
      MessageDlg(_('Cannot be edited. Tickets are sold for this tournament.'), mtInformation, [mbOK], 0);
      exit;
    end;
    TableNm := 'TOURNAMENT_ROUNDS';
    Query := 'SELECT MAX(SEQ_INDEX) FROM ' + TableNm + ' WHERE TOURNAMENT_ID=' + FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row];
  end
  else
  begin
    TableNm := 'TOURNAMENT_ROUND_TYPE_DETAILS';
    Query := 'SELECT MAX(SEQ_INDEX) FROM ' + TableNm + ' WHERE ROUND_TYPE_ID=' + FrmMain.stg_RoundTypes.Cells[0, FrmMain.stg_RoundTypes.Row];
  end;
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := (Query);
  OracleQuery1.Execute;
  SequenceIdx := OracleQuery1.Field(0);
  edt_Duration.Text := '';
  edt_Ante.Text := '';
  edt_Sequence.Text := inttostr(SequenceIdx + 1);
  edt_SmallBlind.Text := '';
  edt_BigBlind.Text := '';
  chb_IsBreak.Checked := false;
  if IsEdit then
  begin
    Query := 'select * from ' + TableNm + ' where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    edt_Duration.Text := OracleQuery1.Field('DURATION');
    if m_IsClone then
    begin
      edt_Sequence.Text := inttostr(SequenceIdx + 1);
    end
    else
    begin
      edt_Sequence.Text := OracleQuery1.Field('SEQ_INDEX');
    end;
    edt_Ante.Text := OracleQuery1.Field('ANTE');
    edt_SmallBlind.Text := OracleQuery1.Field('SMALL_BLIND');
    edt_BigBlind.Text := OracleQuery1.Field('BIG_BLIND');
    chb_IsBreak.Checked := StrToB(OracleQuery1.Field('IS_BREAK'));
  end;
  // ------
  if m_IsRoundTypes then
    Grid := FrmMain.stg_RoundTypeRounds
  else
    Grid := FrmMain.stg_TournamentRounds;
  if (Grid.Cells[7, Grid.Row] <> Grid.Cells[7, Grid.Row + 1]) and (Grid.Cells[7, Grid.Row + 1] <> '') then
  begin
    chb_LastRebuyRound.Checked := true;
  end
  else
  begin
    chb_LastRebuyRound.Checked := false;
  end;
  // -----------
  if not m_IsRoundTypes then
  begin
    Query := 'SELECT REBUY_ALLOWED FROM TOURNAMENTS WHERE ID=' + FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row];
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    m_RebuyAllowed := StrToB(OracleQuery1.Field(0));
    if m_RebuyAllowed then
    begin
      chb_LastRebuyRound.Visible := true;
    end
    else
    begin
      chb_LastRebuyRound.Visible := false;
    end;
  end
  else
  begin
    // chb_LastRebuyRound.Visible := true;
  end;
  ShowModal();
end;

end.
